package com.edu.hre.db.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;



import com.edu.hre.db.config.BaseDAO;
import com.edu.hre.db.config.DBPool;
import com.edu.hre.db.dto.HreEmpEmergencyContact;
import com.edu.hre.db.dto.HreEmpDependent;


public class EmpDependentDAO  extends BaseDAO {
	private static final Logger logger = Logger.getLogger(EmpDependentDAO.class);
	@SuppressWarnings("deprecation")
	public List<HreEmpDependent> getDependentByEmployeeId(
			int employeeNumber) {
		List<HreEmpDependent> listDependents = new ArrayList<HreEmpDependent>();
		Connection conn = null;
		PreparedStatement preStmt = null;
		ResultSet rs = null;
		try {
			conn = DBPool.getConnection();
			String strSQLQuery = "SELECT * FROM hre_emp_dependents WHERE emp_number = "
					+ employeeNumber;
			preStmt = conn.prepareStatement(strSQLQuery);
			rs = preStmt.executeQuery();
			while (rs.next()) {
				HreEmpDependent empDependent = new HreEmpDependent();
				empDependent.setEdName(rs.getString("ed_name"));
				empDependent.setEdRelationshipType(rs.getString("ed_relationship_type"));
				Date d = null;
				if(rs.getDate("ed_date_of_birth") != null){
					d= (rs.getDate("ed_date_of_birth"));
					d.setYear(d.getYear()-1900);
				}
				empDependent.setEdDateOfBirth(d);
				//.....
				//.....
				listDependents.add(empDependent);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		} finally {
			// đóng PreparedStatement, ResultSet và đẩy connection vào pool
			releaseConnection(conn, preStmt, rs);
		}
		return listDependents;
	}

}
